﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Data;
using App.Core.Extensions;
using System.Text;

public partial class Pages_Public_ProjectList : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindProjectList();
        }
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void BindProjectList()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var projects = from P in context.Projects select P;
        String listItemHtml = @"<div class='ListItem'>                                    
                                    <div class='ListTitle'><a href='/Pages/Public/ProjectDetails.aspx?ID={0}'>{1}</a></div>
                                    <div class='ListSubTitle'><b>Location:</b> {2} <b>Price Per {3}:</b> {4}</div>
                                    <div style='margin-top:5px;'>
                                        {5}{6}
                                        <div class='ListBotter'><b>Developer: </b><span>{7}</span></div>                
                                    </div>                                    
                                </div>";
        String thumbImage = "<img src='" + AppConstants.PROJECT_FILES + "/{0}/{1}' alt='' title='' align='right' class='ThumbInList' />";
        StringBuilder sb = new StringBuilder(10);
        foreach (Project project in projects)
        {
            String priceHtml = String.Empty;
            String thumnail = String.Empty;
            if (project.PricePerUnitFrom > 0 && project.PricePerUnitTo > 0 & project.ShowPricingInfo)
                priceHtml = String.Format("From {0} To {1}", WebUtil.FormatData(project.PricePerUnitFrom), WebUtil.FormatData(project.PricePerUnitTo));
            else
                priceHtml = "Negotiatable";
            
            ProjectFile projectFile = project.ProjectFiles.FirstOrDefault(P => P.FileTypeID != ConfigReader.ProjectBrocheureFileTypeID);
            if (projectFile != null && !projectFile.ThumbFileName.IsNullOrEmpty())             
                thumnail = String.Format(thumbImage, project.ID, projectFile.ThumbFileName);

            sb.AppendFormat(listItemHtml, project.ID, project.Name.HtmlEncode(),
                String.Format("{0}, {1}", project.Zone.City.Name, project.Zone.Name),
                project.Unit.Name, priceHtml,
                thumnail, GetDisplayableProjectDescription(project),
                project.Customer.Name.HtmlEncode());                
        }
        ltrListing.Text = sb.ToString();
    }

    protected String GetDisplayableProjectDescription(Project project)
    {         
        if (project.Description.WordCount() > ConfigReader.ProjectListDescriptionWordCount)
            return String.Format("{0} &nbsp;&nbsp;<a href='/Pages/Public/ProjectDetails.aspx?ID={1}'>Details</a>", WebUtil.FormatText(project.Description.GetWords(ConfigReader.ProjectListDescriptionWordCount)), project.ID);

        return WebUtil.FormatText(project.Description);

    }    
}